package com.atguigu.gmall.list.controller.api;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.SearchService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseAttrVo;
import com.atguigu.gmall.model.list.SearchResponseVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("api/list")
@SuppressWarnings("all")
public class ListApiController {


    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @GetMapping("/inner/createIndex")
    @ApiOperation("与es建立映射关系")
    public Result createIndex(){
        elasticsearchRestTemplate.createIndex(Goods.class);
        elasticsearchRestTemplate.putMapping(Goods.class);
        return Result.ok();
    }
    /**
     * 上边这种方法也是上传es的,但是已经过时了,每个数据还要自己敲
     * 下方的upperGoods这个是最新的方式,在service的实现类中有一个GoodsRepository
     * 继承了ElasticsearchRepository,更方便的操作es,里边有许多方法,直接调用即可
     */



    @Autowired
    private SearchService searchService;

    /**
     * /api/list/inner/upperGoods/{skuId}
     * 商品搜索之上架
     * 就是从数据库中读取然后存储到es中
     * Get
     * @param skuId
     * @return
     */
    @GetMapping("/inner/upperGoods/{skuId}")
    @ApiOperation("商品搜索之上架")
    public Result upperGoods(@PathVariable Long skuId){
        searchService.upperGoods(skuId);
        return Result.ok();
    }

    /**
     * api/list
     * 商品搜索
     * POST
     * @return
     */
    @PostMapping
    @ApiOperation("商品搜索")
    public Result list(@RequestBody SearchParam searchParam){
        SearchResponseVo response=searchService.search(searchParam);
        return Result.ok(response);
    }

    /**
     * /api/list/inner/lowerGoods/{skuId}
     * 商品搜索之下架
     * 删除es中的数据
     * @param skuId
     * @return
     */
    @GetMapping("/inner/lowerGoods/{skuId}")
    @ApiOperation("商品搜索之下架")
    public Result lowerGoods(@PathVariable Long skuId){
        searchService.lowerGoods(skuId);
        return Result.ok();
    }

    /**
     * /api/list/inner/incrHotScore/{skuId}
     * 更新商品的热度排名
     * Get
     * @param skuId
     * @return
     */
    @GetMapping("/inner/incrHotScore/{skuId}")
    @ApiOperation("更新商品的热度排名")
    public Result incrHotScore(@PathVariable Long skuId){
        searchService.incrHotScore(skuId);
        return Result.ok();
    }
}
